package cn.sdormitory.basedata.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.dao.DormitoryBedDao;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.entity.DormitoryBed;
import cn.sdormitory.basedata.service.BDormitoryService;
import cn.sdormitory.basedata.vo.BDormitoryVO;
import cn.sdormitory.common.constant.CommonConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2020/11/7 20:22
 * @version：V1.0
 */
@Slf4j
@Service("bDormitoryService")
public class BDormitoryServiceImpl extends ServiceImpl<BDormitoryDao, BDormitory> implements BDormitoryService {
    @Resource
    private BDormitoryDao bDormitoryDao;

    @Autowired
    DormitoryBedDao dormitoryBedDao;

    @Autowired
    BStudentDao bStudentDao;

    @Override
    public IPage<BDormitory> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        String buildingNo = (String) params.get("buildingNo");
        String storey = (String) params.get("storey");
        String dormitoryNo = (String) params.get("dormitoryNo");
        String status = (String) params.get("status");

        LambdaQueryWrapper<BDormitory> wrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotEmpty(buildingNo)) {
            wrapper.eq(BDormitory::getBuildingNo, buildingNo);
        }
        if (StrUtil.isNotEmpty(storey)) {
            wrapper.eq(BDormitory::getStorey, storey);
        }
        if (StrUtil.isNotEmpty(dormitoryNo)) {
            wrapper.eq(BDormitory::getDormitoryNo, dormitoryNo);
        }
        if (StrUtil.isNotEmpty(status)) {
            wrapper.eq(BDormitory::getStatus, status);
        }

        wrapper.apply(params.get(CommonConstant.SQL_FILTER) != null, (String) params.get(CommonConstant.SQL_FILTER));
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public BDormitory getBDormitoryById(Long id) {
        BDormitory dormitory = getById(id);
        if(dormitory == null){
            return null;
        }
        LambdaQueryWrapper<DormitoryBed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DormitoryBed::getDormitoryId,id);
        List<DormitoryBed> dormitoryBeds = dormitoryBedDao.selectList(wrapper);
        dormitory.setBedList(dormitoryBeds);
        return dormitory;
    }

    @Override
    public int create(BDormitory bDormitory) {
        int num = this.baseMapper.insert(bDormitory);
        List<DormitoryBed> bedList = bDormitory.getBedList();
        for(DormitoryBed item : bedList){
            item.setDormitoryId(bDormitory.getId());
            item.setStatus("1");
            item.setBedType(item.getBedType());
            dormitoryBedDao.insert(item);
        }
        return num;
    }

    @Override
    public int delete(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int update(Long id, BDormitory bDormitory) {
        bDormitory.setId(id);
        bDormitory.setModifyTime(new Date());

        //删除原本的床位
        LambdaQueryWrapper<DormitoryBed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DormitoryBed::getDormitoryId,id);
        dormitoryBedDao.delete(wrapper);

        List<DormitoryBed> bedList = bDormitory.getBedList();
        for(DormitoryBed item : bedList){
            item.setDormitoryId(id);
            item.setStatus("1");
            item.setBedType(item.getBedType());
            dormitoryBedDao.insert(item);
        }

        return this.baseMapper.updateById(bDormitory);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        for(Long dormitoryId : idList){
            LambdaQueryWrapper<DormitoryBed> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DormitoryBed::getDormitoryId,dormitoryId);
            dormitoryBedDao.delete(wrapper);
        }
        return this.baseMapper.deleteBatchIds(idList);
    }

    @Override
    public int updateStatus(Long id, String status) {
        BDormitory bDormitory = new BDormitory();
        bDormitory.setId(id);
        bDormitory.setStatus(status);
        return this.baseMapper.updateById(bDormitory);
    }

    @Override
    public BDormitory getByDormitoryInfo(String buildingNo, String storey, String dormitoryNo) {
        LambdaQueryWrapper<BDormitory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BDormitory::getBuildingNo, buildingNo);
        wrapper.eq(BDormitory::getStorey, storey);
        wrapper.eq(BDormitory::getDormitoryNo, dormitoryNo);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public List<BDormitory> getListAll() {
        return list(new LambdaQueryWrapper<BDormitory>());
    }

    @Override
    public List<BDormitory> getBDormitoryListByCol(String buildingNo, String storey) {
        return bDormitoryDao.getBDormitoryListByCol(buildingNo,storey);
    }

    @Override
    public List<BDormitory> getBDormitoryList(String buildingNo, String storey, String dormitoryNo, String status, int pageNum,int pageSize) {
        int currIndex=(pageNum-1)*pageSize;
        return bDormitoryDao.getBDormitoryList(buildingNo,storey,dormitoryNo,status,currIndex,pageSize);
    }

    @Override
    public List<BDormitoryVO> getAllDormitory() {
        return this.baseMapper.getAllDormitory();
    }

    @Override
    public boolean emptyDormitoryBed(Long dormitoryId) {
        LambdaQueryWrapper<DormitoryBed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DormitoryBed::getDormitoryId,dormitoryId);
        List<DormitoryBed> dormitoryBeds = dormitoryBedDao.selectList(wrapper);
        for(DormitoryBed item : dormitoryBeds){
            this.dormitoryBedDao.update(item, new UpdateWrapper<DormitoryBed>().lambda()
                    .set(DormitoryBed::getStudentId, null)
                    .eq(DormitoryBed::getId, item.getId()));

            //更新学生表床位信息
            LambdaQueryWrapper<BStudent> stuWrapper = new LambdaQueryWrapper<>();
            stuWrapper.eq(BStudent::getBedId,item.getId());
            BStudent bStudent = bStudentDao.selectOne(stuWrapper);
            if(bStudent != null){
                int update = this.bStudentDao.update(bStudent, new UpdateWrapper<BStudent>().lambda()
                        .set(BStudent::getBedId, null).set(BStudent::getBedNo, null)
                        .eq(BStudent::getId, bStudent.getId()));

            }
        }
        return true;
    }

    @Override
    public List<DormitoryBed> getDormitoryBedList(long dormitoryId) {
        LambdaQueryWrapper<DormitoryBed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DormitoryBed::getDormitoryId,dormitoryId);
        wrapper.eq(DormitoryBed::getStatus,"1");
//        wrapper.isNull(DormitoryBed::getStudentId);
        List<DormitoryBed> dormitoryBeds = dormitoryBedDao.selectList(wrapper);
        return dormitoryBeds;
    }
}
